package cn.com.huzo.webapp.controller.oa;

import cn.com.huzo.model.entities.oa.BusinessCardEntity;
import cn.com.huzo.service.oa.BusinessCardManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 名片夹
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/oa/business_card")
public class BusinessCardController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "BusinessCardManager")
    private BusinessCardManager _manager;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到名片夹一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("oa.business_card.list.def");
        return view;
    }

    /**
     * 跳转到名片夹编辑画面
     *
     * @param action 操作方法
     * @param businessCardId 名片夹ID
     * @return
     */
    @RequestMapping("/{action}/{businessCardId}/edit.html")
    public ModelAndView redirectEditPopupWnd(@PathVariable(value = "action") int action,
            @PathVariable(value = "businessCardId") String businessCardId) {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量        
        view.addObject("action", action);
        if (!"0".equals(businessCardId)) {
            view.addObject("bulletinId", businessCardId);
        }
        view.setViewName("oa.business_card.edit.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 取得该名片夹详细。
     *
     * @param criterion 用户标识等
     * @return 名片夹数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/fetchByKey.html",
            method = RequestMethod.POST)
    @ResponseBody
    public BusinessCardEntity fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.fetchByKey(MapUtils.getString(criterion, "BusinessCardId"));
    }

    /**
     * 根据条件取得名片夹信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchForPage(criterion);
    }

    /**
     * 新建名片夹数据。
     * <p>
     * @param instance 新建的名片夹。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> insert(@RequestBody BusinessCardEntity instance) throws Exception {
        Map<String, Object> _result = new HashMap();
        _result.put("Key", _manager.insert(instance));
        return _result;
    }

    /**
     * 更新名片夹数据。
     * <p>
     * @param instance 新建的名片夹。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer update(@RequestBody BusinessCardEntity instance) throws Exception {
        return _manager.update(instance);
    }

    /**
     * 删除。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.delete(criterion);
    }
    // </editor-fold>
}
